(function($, im, doc) {
	$.plusReady(function() {
        im.areaMsgList  = doc.querySelector('#msg-list');
//      im.record       = [];
        window.addEventListener('SENDCARD', function(event) {
            var uinfo = event.detail;
            document.querySelector('#sendProfile').style.display = 'block';
            document.querySelector('#toName span').innerText = im.chatName;
            document.querySelector('#toName img').setAttribute('src',im.header);
            document.querySelector('#fromName .span1').innerText = '[个人名片]';
            document.querySelector('#fromName .span2').innerText = uinfo.fname;
            document.querySelector('#fromName').setAttribute('fid',uinfo.fid);
            document.querySelector('#fromName').setAttribute('head',uinfo.head);
            document.querySelector('#fromName').setAttribute('title',uinfo.title);
            document.querySelector('#fromName').setAttribute('phone',uinfo.phone);
            document.querySelector('#fromName').setAttribute('pattern','card');
            document.querySelector('#fromName').getAttribute('c_msg',uinfo.c_msg);
            document.querySelector('#fromName').getAttribute('c_img',uinfo.c_img);
        });
		document.getElementById("totags").addEventListener('tap', function () {
			//plus.webview.hide('chatbox');
			
            mui.openWindow({
                url: "friendlist.html",
                id: "friendlist.html",
                extras: im.extraWin
            });

		});
        mui("#sendProfile").on("tap", "#cancel", function() {
            document.querySelector('#sendProfile').style.display = 'none';
        });

        mui("#sendProfile").on("tap", "#send", function() {
            var pattern   =  document.querySelector('#fromName').getAttribute('pattern');
            if(pattern == 'card'){
                var tname   = document.querySelector('#toName span').innerText;
                var fname   = document.querySelector('#fromName span').innerText;
                var header  = document.querySelector('#toName img').getAttribute('src');
//              var content = document.querySelector('#msg-text2').innerText;
                var fid     =  document.querySelector('#fromName').getAttribute('fid');
                var head    =  document.querySelector('#fromName').getAttribute('head');
                var phone   =  document.querySelector('#fromName').getAttribute('phone');

                var extras  = {
                    pattern: 'card',
                    name: fname,
                    fid: fid,
                    head: head,
                    phone: phone
                };
                var sendmsg = {
                    sender: im.tomsgsender,
                    header: header,
                    type: 'person',
                    name: tname,
//                  content: content,
                    time: '刚刚',
                    extras: extras
                };
                im.sendMsg(sendmsg);
            }else if(pattern == 'fav'){
                var tname   = document.querySelector('#fromName').innerText;
                var fname   = document.querySelector('#fromName span').innerText;
                var header  = document.querySelector('#toName img').getAttribute('src');
//              var content = document.querySelector('#msg-text2').innerText;
                var fid     =  document.querySelector('#fromName').getAttribute('fid');
                var head    =  document.querySelector('#fromName').getAttribute('head');
                var phone   =  document.querySelector('#fromName').getAttribute('phone');
                var c_msg   =  document.querySelector('#fromName').getAttribute('c_msg');
                var c_img   =  document.querySelector('#fromName').getAttribute('c_img');
                if(c_msg){
                    var type = 'text';
                }else if(c_img){
                    var type = 'image';
                }
                var extras  = {
                    pattern: 'fav',
                    fid: fid,
                    head: head,
                    phone: phone,
                    c_msg: c_msg,
                    c_img: c_img
                };
                var sendmsg = {
                    sender: im.tomsgsender,
                    header: header,
                    type: type,
                    name: tname,
//                  content: content,
                    time: '刚刚',
                    extras: extras
                };
                im.sendMsg(sendmsg);
            }
			im.closeFooterBox();
            /*
            var msgInfo = {
                type: 'say',
                user_id: im.uid,
                user_name: app.getState().name,
                user_header: app.getState().avatar ? app.getState().avatar : '',
                room_id: im.roomId,
                chat_name: im.chatName,
                header: im.header,
                msg: content,
                ftype: 'person', //聊天类型
                ctype: im.roomType, //聊天室类型
                extras: extras
            };*/
            // im.addnewMsg(msgInfo);
            // im.record.push(sendmsg);
            
            // im.bindMsgList();
            // im.toRobot(msgInfo);
            /*
            im.areaMsgList.innerHTML = template('msg-template', {
                "record": im.record
            });*/
            document.querySelector('#sendProfile').style.display = 'none';
        });

        mui("#msg-list").on("tap", ".mycard", function() {
            var fid = this.getAttribute('fid');
            var head = this.getAttribute('head');
            var title = this.getAttribute('title');
            var phone = this.getAttribute('phone');
            //alert(fid);
            //alert(head);
            //alert(title);
            //alert(phone);
            
            //plus.webview.open("/pages/user/other_detail.html");
            //plus.webview.currentWebview().close();
            var service_comfirm = plus.webview.getWebviewById("/pages/user/other_detail.html");
			service_comfirm && plus.webview.close(service_comfirm, "none");
            mui.openWindow({
                url: "/pages/user/other_detail.html",
                id: "/pages/user/other_detail.html",
                extras: {
                    fid: fid,
                    head: head,
                    fname: title,
                    phone : phone
                }
            });
        })

	});
}(mui, im, document));